/*
 *  JVending - JSP Tag Library
 *  Copyright (C) 2004  Shane Isbell
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.jvending.provisioning.taglib;


import java.util.Iterator;
import java.util.List;
import java.util.logging.*;

import javax.provisioning.ProvisioningContext;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.BodyContent;

/**
 * @author Shane Isbell
 * @since 1.3.9a
 */

public class CapabilityValuesTag extends ProvisioningBaseTag {

    private static Logger logger = Logger.getLogger("CapabilityValuesTag");

    private Iterator capabilityValues;

    private String capabilityName;

    public String getCapabilityName() {
        return capabilityName;
    }

    public void setCapabilityName(String capabilityName) {
        this.capabilityName = capabilityName;
    }

    public int doStartTag() throws JspTagException {
        ProvisioningContext provisioningContext = getProvisioningContext();
        //TO DO: not set from parameter
        capabilityName = (String) pageContext.getAttribute("capabilityName", pageContext.PAGE_SCOPE);
        List capabilityValue = (List) pageContext.getAttribute("capabilityValues", pageContext.PAGE_SCOPE);

        if(provisioningContext == null || capabilityName == null || 
           capabilityValue == null || capabilityValue.size() == 0) return SKIP_BODY;

        capabilityValues = capabilityValue.iterator();

        pageContext.setAttribute("capabilityValue", capabilityValues.next());
        return EVAL_BODY_BUFFERED;
    }

    protected String getAttribute() {
        return "capabilityValue";
    }

    protected Iterator getIterator() {
        return capabilityValues;
    }


}
